package stage;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

public class Renderer extends Thread {

    private int width = 750;
    private int height = 500;
    
    Renderer(int width, int height) {
	setDimensions(width, height);
    }
    
    
    @Override
    public void run() {

	try {
	    //Setup display
	    Display.setDisplayMode(new DisplayMode(width, height));
	    Display.create();
	    Display.setTitle("Ball Battles");
	    
	} catch (LWJGLException e) {
	    e.printStackTrace();
	    System.exit(1);
	}

	initOpenGL();
	
	//Rendering loop
	while(!Display.isCloseRequested()) {
	    draw();
	    Display.update();
	    
	}
	
	//Exit code
	Display.destroy();
	
    }

    private void setDimensions(int width, int height) {
	this.width = width;
	this.height = height;
    }

    /**
     * Initialize OpenGL
     */
    private void initOpenGL() {
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, width, height, 0, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
    }

    /**
     * Draw 
     */
    private void draw() { 
        // Clear the screen and depth buffer
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);	

        // set the color of the quad (R,G,B,A)
        GL11.glColor3f(0.5f,0.5f,1.0f);

        // draw quad
        GL11.glBegin(GL11.GL_QUADS);
            GL11.glVertex2f(100,100);
            GL11.glVertex2f(100+200,100);
            GL11.glVertex2f(100+200,100+200);
            GL11.glVertex2f(100,100+200);
        GL11.glEnd();

        //Rotate
        GL11.glPopMatrix();
        
    }
}
